// Purpose : This page is use for show message posted by 
// a authenticated user in respect a question for topics
// Author  : Vinayak Singh Rathore 
// Date    : 12 Sep, 2007
using System;

using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using SiteManager;

public partial class Community_PostRespose : System.Web.UI.Page
{
    // variable is use for show topic name on header
    protected string TopicName;
    protected string TopicTitle = "";
    protected string Message;// this variable is use for show message
    //string reqmid = ""; commented at 131008 to fix warning
    //string reqid = ""; commented at 131008 to fix warning
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            // if user is not authenticated then unable to post message
            //if (Session["userID"] == null || Session["userID"].ToString().Length <= 0)
            //{
            //    PostMessage.Visible = false;

            //}
            if (Request.QueryString["Id"] == null || Request.QueryString["Mid"] == null)
            {
                Response.Redirect(Session["strHttp"] + "/Community/Community.aspx");
            }
            try
            {
                if (Request.QueryString["Id"] != null && Manager.IsNumeric(Request.QueryString["Id"]) == false)
                {
                    Response.Redirect(Session["strHttp"] + "/Community/Community.aspx");
                }
                if (Request.QueryString["Mid"] != null && Manager.IsNumeric(Request.QueryString["Mid"]) == false)
                {
                    Response.Redirect(Session["strHttp"] + "/Community/Community.aspx");
                }
            }
            catch
            {
                Response.Redirect(Session["strHttp"] + "/Community/Community.aspx");
            }
            BindReapeter();
            ShowMessage();
            SetTopicName();            
        }
        txtDescription.RootPath = Session["strHttp"] + "includes/Editor/";        
        BindReapeter();
        ShowMessage();
        SetTopicName();        
        btnSubmit.Attributes.Add("OnClick", "return CheckForumMessageIsBlank('" + txtDescription.ClientID + "','" + lblMessage.ClientID + "');");        
    }
    /// <summary>
    /// submit button event handler 
    /// </summary>
    /// <author>Vinayak Singh Rathore</author>
    /// <date>14 Sep, 2007</date>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSubmit_Click1(object sender, EventArgs e)
    {
        //ADDED BY RANJEET AT 22/02/2008 IF ANYONE POST TOPIC WITHOUT LOGIN THEN REDIRECT LOGIN PAGE.
        if (Session["userID"] == null || Session["userID"].ToString().Length <= 0)
        {
            lblMessage.Text = "You can not post message without login";
            return;
        }
        if (txtDescription.EditorContent.ToString() == "")
        {
            lblMessage.Visible = true;
            lblMessage.Text = "Post message can't be blank.";
            return;
        }
        //END
        Manager objManger = new Manager(true,"ConnectionString");
        int userType = 1;
        if ((Session["userType"].ToString() == "Doctor") && ((Convert.ToString(Session["DoctorType"]).ToString() == "Doctor")))
            userType = 2;
        else if (Session["userType"].ToString() == "SiteUser")
            userType = 3;
        else if ((Session["userType"].ToString() == "Doctor") && (Convert.ToString(Session["DoctorType"]) == "Dentist"))
            userType = 4;

        if (Request.QueryString["Id"] != null && Session["userID"] != null && Session["userID"].ToString().Length > 0)
        {
            SqlParameter[] objParameter = new SqlParameter[8];
            objParameter[0] = new SqlParameter("@TopicID", SqlDbType.BigInt);
            objParameter[0].Value = Convert.ToInt32(Request.QueryString["Id"]);
            objParameter[1] = new SqlParameter("@ParentID", SqlDbType.BigInt);
            objParameter[1].Value = Convert.ToInt32(Request.QueryString["Mid"]);
            objParameter[2] = new SqlParameter("@AuthorID", SqlDbType.BigInt);
            objParameter[2].Value = Convert.ToInt32(Session["userID"]);
            objParameter[3] = new SqlParameter("@AuthorType", SqlDbType.BigInt);
            objParameter[3].Value = userType;
            objParameter[4] = new SqlParameter("@Msg", SqlDbType.Text);
            objParameter[4].Value = txtDescription.EditorContent.ToString();
            objParameter[5] = new SqlParameter("@IsError", SqlDbType.Bit);
            objParameter[5].Direction = ParameterDirection.Output; 
           
            objParameter[6] = new SqlParameter("@DoctorID", SqlDbType.BigInt);
            objParameter[6].Value = Convert.ToInt32(Session["userID"]);
            objParameter[7] = new SqlParameter("@BlogTitle", SqlDbType.Text);
            objParameter[7].Value = TopicName;
            

            objManger = new Manager(true,"ConnectionString");
            objManger.ExecuteQuery("AddForumandRespose", objParameter);
            if (Convert.ToBoolean(objParameter[5].Value))
            {
                lblMessage.Text = "You are not valid user.";
            }
            else
            {
                lblMessage.Text = "Message has been Posted ";
                txtDescription.EditorContent = "";               
            }
            objManger = null;
            



        }
        else
        {

            lblMessage.Text = "Login for insert Question";
        }
        objManger = null;
        BindReapeter();
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
       
    }
    public void BindReapeter()
    {
        SqlParameter[] objParameter = new SqlParameter[1];
        objParameter[0] = new SqlParameter("@MID", SqlDbType.BigInt);
        objParameter[0].Value = Convert.ToInt32(Request.QueryString["Mid"]);
        pagingForum.SqlParameterList = objParameter;
    }

    /// <summary>
    /// This function is use for fetch the message from data base and show them in litral controls
    /// </summary>
    public void ShowMessage()
    {
        Manager objManger = new Manager(true,"ConnectionString");
        SqlDataReader objDataReader = null;
        objDataReader = objManger.ExecuteQuery("select msg from forum where id=" + Convert.ToInt32(Request.QueryString["Mid"]));
        if (objDataReader.HasRows)
        {
            objDataReader.Read();
            Message = objDataReader["msg"].ToString();
        }
        objManger = null;
           
    }
    // This function return  the topic name for showing in header
    public void SetTopicName()
    {
       Manager objManger = new Manager(true,"ConnectionString");
        SqlDataReader objDataReader = null;
        objDataReader = objManger.ExecuteQuery("select name from tbl_topics where id=" + Convert.ToInt32(Request.QueryString["Id"]));
        if (objDataReader.HasRows)
        {
            objDataReader.Read();
            TopicName = objDataReader["name"].ToString();
        }
        objManger = null;

    }
    

    }

